package com.example.video.admin_server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.video.admin_server.entity.SysRole;
import com.example.video.admin_server.entity.SysRoleMenu;
import com.example.video.admin_server.entity.vo.SysRoleDetailsVo;
import com.example.video.admin_server.mapper.SysRoleMapper;
import com.example.video.admin_server.service.ISysRoleMenuService;
import com.example.video.admin_server.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统角色表 服务实现类
 * </p>
 *
 * @author yj
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private ISysRoleMenuService iSysRoleMenuService;

    @Transactional
    public boolean saveRole(SysRoleDetailsVo sysRoleDto) {
        // 新增角色
        boolean save = this.save(sysRoleDto);
        // 角色新增菜单
        boolean b = addRoleMenu(sysRoleDto);
        return b && save;
    }

    @Override
    @Transactional
    public boolean updateRole(SysRoleDetailsVo sysRoleDto) {
        // 修改角色
        boolean save = this.updateById(sysRoleDto);
        // 删除角色菜单
        boolean remove = iSysRoleMenuService.remove(new QueryWrapper<>(new SysRoleMenu().setRoleId(sysRoleDto.getId())));
        //角色新增菜单
        boolean b = addRoleMenu(sysRoleDto);
        return  save && remove;
    }

    @Override
    public SysRoleDetailsVo getDetails(Integer id) {
        SysRole byId = this.getById(id);
        List<SysRoleMenu> list = iSysRoleMenuService.list(new QueryWrapper<>(new SysRoleMenu().setRoleId(id)));
        List<Integer> menuIds = new ArrayList<>();
        for (SysRoleMenu sysRoleMenu : list){
            menuIds.add(sysRoleMenu.getMenuId());
        }
        SysRoleDetailsVo sysRoleDetailsVo = new SysRoleDetailsVo(byId);
        sysRoleDetailsVo.setMenuIds(menuIds);
        return sysRoleDetailsVo;
    }

    @Override
    @Transactional
    public boolean delRole(Integer roleId) {
        boolean b = this.removeById(roleId);
        boolean remove = iSysRoleMenuService.remove(new QueryWrapper<>(new SysRoleMenu().setRoleId(roleId)));
        return b&&remove;
    }

    private boolean addRoleMenu(SysRoleDetailsVo sysRoleDto){
        List<Integer> addMenuIds = sysRoleDto.getMenuIds();
        boolean add = true;
        if (addMenuIds != null && addMenuIds.size() > 0 ) {
            List<SysRoleMenu> sysRoleMenus = new ArrayList<>();
            SysRoleMenu sysRoleMenu = null;
            for (Integer menuId : addMenuIds) {
                sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(sysRoleDto.getId()).setMenuId(menuId);
                sysRoleMenus.add(sysRoleMenu);
            }
            add = iSysRoleMenuService.saveBatch(sysRoleMenus);
        }
        return add;
    }
}
